﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarPointsonPremiumDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarPointsonPremiumDB
    {
        public CarPointsonPremiumDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarPointsonPremium CarPointsonPremium)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarPointsonPremiumInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarPointsId", CarPointsonPremium.ProviderCarPointsId);
            _sPara.Add("@ProviderCompanyId", CarPointsonPremium.ProviderCompanyId);
            _sPara.Add("@Points", CarPointsonPremium.Points);
            _sPara.Add("@Description", CarPointsonPremium.Description);
            _sPara.Add("@Premium", CarPointsonPremium.Premium);
            _sPara.Add("@RefCEUserId", CarPointsonPremium.RefCEUserId);
            _sPara.Add("@Status", CarPointsonPremium.Status);
            _sPara.Add("@Finfo1", CarPointsonPremium.FInfo1);
            _sPara.Add("@Dinfo1", CarPointsonPremium.DInfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CarPointsonPremium GetProviderCarPointsonPremium(Int64 ProviderCarPointsId)
        {
            CarPointsonPremium _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarPointsonPremiumSelectSingleItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarPointsId", ProviderCarPointsId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarPointsonPremium();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        public CarPointsonPremium GetProviderCarPointsonPremium(Int64 ProviderCompanyId, Int32 Points)
        {
            CarPointsonPremium _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarPointsonPremiumSelectSingleItemSelected]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@Points", Points);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarPointsonPremium();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        private CarPointsonPremium FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarPointsonPremium myCarPointsonPremium = new CarPointsonPremium();
            myCarPointsonPremium.ProviderCarPointsId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarPointsId"].ToString());
            myCarPointsonPremium.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarPointsonPremium.Points = Convert.ToInt32(myDataTable.Rows[RowId]["Points"].ToString());
            myCarPointsonPremium.Description = myDataTable.Rows[RowId]["Description"].ToString();
            myCarPointsonPremium.Premium = Convert.ToSingle(myDataTable.Rows[RowId]["Premium"].ToString());
            myCarPointsonPremium.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarPointsonPremium.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarPointsonPremium.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarPointsonPremium.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarPointsonPremium.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarPointsonPremium.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            return myCarPointsonPremium;
        }
    }
}